<?php
declare (strict_types=1);

namespace app\listener\agent;

use app\event\AgentAddSuccess;
use app\model\Agent;
use app\model\AgentChannel;
use app\model\ChannelServDiscount;
use think\facade\Db;


class AgentAddSuccessYmsh
{
    /**
     * 事件监听处理
     * 在创建代理商的事件的时候(关联有美的is_relation为1的)添加一个事件，复制一份xiaochengxu的本地生活的渠道，渠道名为随机不重复
     * @param AgentAddSuccess $event
     * @return mixed
     */
    public function handle(AgentAddSuccess $event)
    {
        /**
         * @var Agent $agent
         */
        $agent = $event->agent;
        if ($agent->is_relation != Agent::IS_RELATION_YES) {
            return;
        }
        $channelInfo = AgentChannel::query()->where('name', AgentChannel::YMSH_CHANNEL_XIAOCHENGXU)->find();
        if (!$channelInfo) {
            return;
        }
        //todo 拷贝一份有美生活小程序的渠道信息
        /**
         * @var AgentChannel $newChannel
         */
        $newChannel = clone $channelInfo;
        $old_channel_id = $newChannel->id;
        $newChannelArr = $newChannel->toArray();
        unset($newChannelArr['id']);
        unset($newChannelArr['agent_id']);
        unset($newChannelArr['name']);
        unset($newChannelArr['created_at']);
        unset($newChannelArr['updated_at']);
        unset($newChannelArr['template']);
        unset($newChannelArr['template_url']);
        unset($newChannelArr['is_sys']);
        $newChannelArr['is_sys'] = AgentChannel::IS_SYS_ACTIVE;
        $newChannelArr['channel_type'] = AgentChannel::CHANNEL_TYPE_YMSH;
        $newChannelArr['agent_id'] = $agent->id;
        Db::startTrans();
        try {
            //todo 渠道名称唯一，并且是随机的
            $newChannelArr['name'] = (new AgentChannel())->getRandomChannelName();
            $newChannelInfo = AgentChannel::create($newChannelArr);
            $channel_id = $newChannelInfo->id;
            //查询xiaochengxu渠道的channel_serv_discount
            $old_channel_serv_discounts = ChannelServDiscount::query()
                ->where('channel_id', $old_channel_id)
                ->select();
            $params = [];
            foreach ($old_channel_serv_discounts as $key => $channel_serv_discount) {
                /**
                 * @var ChannelServDiscount $channel_serv_discount
                 */
                $params[$key]['channel_id']     = $channel_id;
                $params[$key]['serv_type']      = $channel_serv_discount->serv_type;
                $params[$key]['value']          = $channel_serv_discount->value;
                $params[$key]['sub_type']       = $channel_serv_discount->sub_type;
                $params[$key]['state']          = $channel_serv_discount->state;
                $params[$key]['created_at']     = date('Y-m-d H:i:s', time());
                $params[$key]['updated_at']     = date('Y-m-d H:i:s', time());
            }
            $res = (new ChannelServDiscount())->saveAll($params);
            if (!$res) {
                Db::rollback();
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }
}
